package dao

import (
	"encoding/json"
)

type UserLog struct {
	BaseModel
	Type      string
	Describe  string
	IpAddress string
	UserId    int
}

func (c *UserLog) TableName() string {
	return "user_log"
}

func (c *UserLog) ToJsonStr() string {
	marshal, err := json.Marshal(c)
	if err != nil {
		return ""
	}
	return string(marshal)
}

func SelectAllUserLog() *[]UserLog {
	userLog := make([]UserLog, 0)
	DB.Where("is_del = ?", 0).Find(&userLog)
	return &userLog
}

func SelectUserLogByExample(userLog *UserLog) *[]UserLog {
	logs := make([]UserLog, 0)
	DB.Where(&userLog)
	DB.Find(&logs)
	return &logs
}

func SaveUserLog(userLog *UserLog) int {
	DB.Create(&userLog)
	return userLog.ID
}

func SaveUserLogBatch(userLogs *[]UserLog) {
	for _, userLog := range *userLogs {
		DB.Create(&userLog)
	}
}

func UpdateUserLog(userLog *UserLog) {
	DB.Model(&userLog).Save(&userLog)
}

func DeleteUserLog(id int) {
	userLog := UserLog{}
	userLog.ID = id
	DB.Delete(userLog)
}

func DeleteUserLogByExample(userLog *UserLog) {
	DB.Where(&userLog).Delete(UserLog{})
}
